package countValidWords;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Solution {
    public int countValidWords(String sentence) {
        //
        int ans = 0;
        String[] words = sentence.split(" ");
        for (int i = 0; i < words.length; i++) {
            if(check(words[i])){
                ans++;
                //  System.out.println(words[i]);
            }
        }
        return ans;
    }
    private boolean check(String word) {
        // 只有一个字母
        if(word.length() == 0) return false;
        if(word.length() == 1) return word.charAt(0)!='-'&&(word.charAt(0)<'0'||word.charAt(0)>'9');
        // 第一个字母是单词
        int biao = 0;
        int lian = 0;
        if(!Character.isLetter(word.charAt(0))) return false;
        for (int i = 1; i < word.length(); i++) {
            if(word.charAt(i) == '-') {
                // 前后都是字母
                lian++;
                if(lian>1) return false;
                if(i == word.length()-1) return false;
                if(!Character.isLetter(word.charAt(i-1))||!Character.isLetter(word.charAt(i+1))) return false;
            } else if (word.charAt(i)>='0'&&word.charAt(i)<='9') {
                return false;
            }else if (!Character.isLetter(word.charAt(i))) {// 标点符号
                biao ++;
                if(biao>1) return false; // 只能有一个标点符号
                if(i!=word.length()-1) return false;
            }
        }
        return true;
    }

}
